home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Linked Lists Template Class 1.1 / Linked Lists 1.1 Ä.sit / Linked Lists 1.1 ƒ / source code / LinkedLists.h < prev    next >
C/C++ Source or Header  |  1995-03-15  |  12KB  |  277 lines

  1. #pragma once
  2.  
  3.  
  4. /****************************************************************************************************
  5. *                                                                                                    *
  6. *    Class ListItem is used internally by the ListClasses; you don't have to worry about it.            *
  7. *    It is simply a template class which will hold the class you're storing in the list and will        *
  8. *    point to the next object in the list                                                            *
  9. *                                                                                                    *
  10. ****************************************************************************************************/
  11.  
  12.  
  13.  
  14.  
  15. template <class itemClass> class ListItem {
  16.  
  17. public:
  18.     itemClass    item;
  19.     ListItem    *next;
  20. };
  21.  
  22.  
  23.  
  24.  
  25.  
  26. /****************************************************************************************************
  27. *                                                                                                    *
  28. *    General List Info                                                                                *
  29. *        Ñ "itemRecord" refers to the specific variable/class/struct type                             *
  30. *          when an instance of this template class is created                                        *
  31. *                                                                                                    *
  32. ****************************************************************************************************/
  33.  
  34.  
  35. /****************************************************************************************************
  36. *                                                                                                    *
  37. *    SlimListClass                                                                                    *
  38. *                                                                                                    *
  39. *    SlimListClass is the most basic of all the Lists, providing simple Push and Pop operations.        *
  40. *    (It will create a stack, with only the top item accessible, no sorting involved.)                *
  41. *                                                                                                    *
  42. *    This class will also "clean up" after itself, deallocating all the memory it allocates when        *
  43. *    it is terminated.                                                                                *
  44. *                                                                                                    *
  45. *                                                                                                    *
  46. *    Function Usage                                                                                    *
  47. *                                                                                                    *
  48. *    Boolean        Push(itemRecord &theItem);                                                            *
  49. *        Ñ will "push" the item on to the top of the stack                                            *
  50. *        Ñ returns 1 if successful, 0 if not (memory couldn't be allocated)                            *
  51. *                                                                                                    *
  52. *    Boolean        Pop(itemRecord &theItem);                                                            *    
  53. *        Ñ "pops" the top item - takes it off the stack and returns true if there is an item            *
  54. *         - Pop will destroy the item in memory, calling the item's destructor - Use with care!        *
  55. *                                                                                                    *
  56. *    void        DeleteAll();                                                                         *
  57. *        Ñ deletes all entries in stack                                                                *
  58. *        Ñ also called when variable is destroyed (e.g., local variable in function dieing when        *
  59. *          function is done)                                                                            *
  60. *                                                                                                    *
  61. *                                                                                                    *
  62. ****************************************************************************************************/
  63.     
  64.  
  65.  
  66.  
  67.  
  68. template <class itemRecord> class SlimListClass {
  69. protected:
  70.     ListItem<itemRecord>    *stackTop;
  71. public:
  72.     SlimListClass()        { stackTop=nil; }
  73.     ~SlimListClass()    { DeleteAll(); }
  74.     Boolean        Push(itemRecord &theItem);
  75.     Boolean        Pop(itemRecord &theItem);
  76.     void        DeleteAll(); 
  77. };
  78.  
  79.  
  80.  
  81.  
  82.  
  83. /****************************************************************************************************
  84. *                                                                                                    *
  85. *    SteppingList                                                                                    *
  86. *                                                                                                    *
  87. *    SteppingList is a list which can be "stepped" through, meaning you can retrieve specific        *
  88. *    items in the list, or cycle through it.  It maintains a placeHolder in the list, meaning        *
  89. *    you can call the GetTopItem(╔) function, which sets the placeHolder to the top of the list,        *
  90. *    then call the GetPlaceItem(╔) function repeatedly to get the next items.  Items can be Pushed    *
  91. *    on to the list (the top of the list) or Added to the bottom of the list.                        *
  92. *                                                                                                    *
  93. *    Function Usage                                                                                    *
  94. *                                                                                                    *
  95. *    Ñ Add, Push, and Pop should all be self-evident; Add adds the item to the end of the list, Push    *
  96. *    adds the item to the top, and Pop removes the top item from the list.  Using Pop will call the     *
  97. *    class's destructor function, so use with care!                                                    *
  98. *                                                                                                    *
  99. *    Boolean    Add(itemRecord &theItem);                                                                *
  100. *    Boolean    Push(itemRecord &theItem);                                                                *
  101. *    Boolean    Pop(itemRecord &theItem);                                                                *
  102. *                                                                                                    *
  103. *    Ñ Delete(╔) will delete the specified item from the list if it can find it.  DeleteAll deletes    *
  104. *    all the items in the list from memory                                                            *
  105. *                                                                                                    *
  106. *    void    Delete(itemRecord theItem);                                                                *
  107. *    void    DeleteAll();                                                                            *
  108. *                                                                                                    *
  109. *                                                                                                    *
  110. *    Ñ These next five functions will retrieve pointers to specific items in this list.  MAKE SURE    *
  111. *    THE RETURN VALUE IS NOT NIL!  If the return value is nil, the pointer is undefined.                *
  112. *                                                                                                    *
  113. *    Ñ GetTopItem(╔) sets the placeHolder to the top of the stack and returns a pointer to the top    *
  114. *    item, if there is one                                                                            *
  115. *                                                                                                    *
  116. *    ListItem<itemRecord>    *GetTopItem(itemRecord * &theItem);                                        *
  117. *                                                                                                    *
  118. *                                                                                                    *
  119. *    Ñ GetNextItem(╔) gets the next item in place.  GetTopItem(╔) and GetNextItem(╔) are nice to use    *
  120. *    to step through a list.  Just call GetTopItem(╔) followed by GetNextItem(╔) until the latter    *
  121. *    returns nil to get all the items in the list                                                    *
  122. *                                                                                                    *
  123. *    ListItem<itemRecord>    *GetNextItem(itemRecord * &theItem);                                    *
  124. *                                                                                                    *
  125. *                                                                                                    *
  126. *    Ñ GetBottomItem(╔) sets the placeHolder to the bottom and returns a pointer to the bottom item,    *
  127. *    if there is one                                                                                    *
  128. *                                                                                                    *
  129. *    ListItem<itemRecord>    *GetBottomItem(itemRecord * &theItem);                                    *
  130. *                                                                                                    *
  131. *                                                                                                    *
  132. *    Ñ GetItemX(╔) will retrieve itemNumber from the list, if there is an nth item                    *
  133. *                                                                                                    *
  134. *    ListItem<itemRecord>    *GetItemX(short itemNumber, itemRecord * &theItem);                        *
  135. *                                                                                                    *
  136. *    Ñ Cycle(╔) will cycle from the item you pass it to the next item and retrieve a pointer to        *
  137. *    that item; if the next item is at the beginning, it will automatically cycle around                *
  138. *                                                                                                    *
  139. *    ListItem<itemRecord>    *Cycle(itemRecord *currentItem, itemRecord * &nextItem);                *
  140. *                                                                                                    *
  141. *                                                                                                    *
  142. *    Ñ ItemInList(╔) will query whether that item is in the list                                        *
  143. *                                                                                                    *
  144. *    ListItem<itemRecord>    *ItemInList(itemRecord *theItem);                                        *
  145. *                                                                                                    *
  146. *                                                                                                    *                                            *
  147. *    Ñ NumItems() returns the number of items in the list                                            *
  148. *                                                                                                    *
  149. *    int        NumItems();                                                                                *
  150. *                                                                                                    *
  151. ****************************************************************************************************/
  152.     
  153.     
  154.  
  155.  
  156. template <class itemRecord> class SteppingList {
  157.  
  158. protected:
  159.     ListItem<itemRecord>    *placeHolder;
  160.     ListItem<itemRecord>    *stackTop;
  161.     ListItem<itemRecord>    *SetToTop()        { placeHolder=stackTop; return stackTop; }
  162.     int    numberOfItems;
  163.     ListItem<itemRecord>    *GetPlaceItem(itemRecord * &theItem);
  164.  
  165. public:
  166.     SteppingList()    { stackTop=placeHolder=nil; }
  167.     
  168.     Boolean    Add(itemRecord &theItem);
  169.     Boolean    Push(itemRecord &theItem);
  170.     Boolean    Pop(itemRecord &theItem);
  171.     
  172.     void    Delete(itemRecord theItem);
  173.     void    DeleteAll();
  174.     
  175.     ListItem<itemRecord>    *GetTopItem(itemRecord * &theItem);
  176.     ListItem<itemRecord>    *GetNextItem(itemRecord * &theItem);
  177.     ListItem<itemRecord>    *GetBottomItem(itemRecord * &theItem);
  178.     ListItem<itemRecord>    *ItemInList(itemRecord *theItem);
  179.     
  180.     ListItem<itemRecord>    *GetItemX(short itemNumber, itemRecord * &theItem);
  181.     ListItem<itemRecord>    *Cycle(itemRecord *currentItem, itemRecord * &nextItem);
  182.  
  183.     int        NumItems();
  184. };
  185.  
  186.  
  187.  
  188.  
  189.  
  190. /****************************************************************************************************
  191. *    IMPORTANT NOTE ABOUT INHERITING TEMPLATE CLASSES:                                                *
  192. *                                                                                                    *
  193. *    When you inherit template classes into another template class, you must create specific            *
  194. *    instances of all the template classes inherited.  For example, creating instances of a            *
  195. *    ComparisonList below would require the following:                                                *
  196. *                                                                                                    *
  197. *    #pragma template SteppingList<Class1>                                                            *
  198. *    #pragma template ComparisonList<Class1, Class2>                                                    *
  199. *                                                                                                    *
  200. ****************************************************************************************************/
  201.  
  202.  
  203.  
  204. /****************************************************************************************************
  205. *    ComparisonList                                                                                    *
  206. *                                                                                                    *
  207. *    ComparisonList is NOT a self-sorting list, but a list in which you can compare your                *
  208. *    stored objects with another type of object.  For example, you may create a window class            *
  209. *    and store a list of your windows in a linked list.  But when you get update/activate events,    *
  210. *    you need to need to call the update function of your window class and need to find the object    *
  211. *    based on a WindowPtr provided by the EventRecord.  To find it, you can create a ComparisonList    *
  212. *    with you window class as the list object and a WindowPtr as the item to compare the object to.    *
  213. *    ComparisonList is a descendent of the SteppingList and as such contains all its functions.        *
  214. *    It also adds the function XInList(╔):                                                            *
  215. *                                                                                                    *
  216. *                                                                                                    *
  217. *    Boolean    XInList(comparisonObject theObject, itemRecord * &theItem);                                *
  218. *        Ñ passed an object to compare theItem with, XInList will return if item is in list            *
  219. *                                                                                                    *
  220. *        Ñ if the comparison object is in the list, theItem parameter will be set to a pointer to    *
  221. *          that object                                                                                *
  222. *                                                                                                    *
  223. *        ÑImportant note: You must have a logic comparison in the itemRecord class for the             *
  224. *        comparison class.  For example, suppose you create ComparisonList<myWindowClass, WindowPtr>.*
  225. *        Within myWindowClass, you must overload the "==" operator for myWindowClass==WindowPtr        *
  226. *        logic tests.                                                                                *
  227. *                                                                                                    *
  228. *        Ñ a pointer is returned so that changes will be made to _that_ object; if you simply used    *
  229. *          a reference variable of the object, a copy of that object would be made so changes to        *
  230. *          the object returned would not be made to the object in the list                            *
  231. *                                                                                                    *
  232. ****************************************************************************************************/
  233.  
  234.  
  235.  
  236.  
  237.  
  238. template <class itemRecord, class comparisonObject> class ComparisonList : public SteppingList<itemRecord> {
  239.  
  240. public:
  241.     Boolean    XInList(comparisonObject theObject, itemRecord * &theItem);
  242. };
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /****************************************************************************************************
  250. *                                                                                                    *
  251. *    SortingList                                                                                        *
  252. *                                                                                                    *
  253. *    SortingList is a self-sorting list.  Provided you have overloaded the logic operators             *
  254. *    ( <, >, ==, != ) in the class being stored, the list will sort itself as items are added        *
  255. *    via AddSort(╔).  It is a descendent of SteppingList.                                            *
  256. *                                                                                                    *
  257. *    Boolean    AddSort(itemRecord &theItem);                                                            *
  258. *        Ñ Using AddSort to enter all items will create an ascending list (lesser objects at            *
  259. *          the stack top, down to the greater items)                                                    *
  260. *                                                                                                    *
  261. *        Ñ Important note: AddSort will sort only the piece that's entered, not the whole list.        *
  262. *          That is, if you Push some items on, Add others, then AddSort an item, the list will        *
  263. *          not be sorted, and the item will be placed after the first object it's greater than        *
  264. *                                                                                                    *
  265. *                                                                                                    *
  266. ****************************************************************************************************/
  267.  
  268.     
  269.  
  270.  
  271. template <class itemRecord> class SortingList : public SteppingList<itemRecord> {
  272.  
  273. public:
  274.     Boolean    AddSort(itemRecord &theItem);
  275. };
  276.  
  277.